package com.rhb.erp.hr.person.dao;

import java.util.*;

import com.rhb.erp.hr.person.bean.*;
import com.rhb.util.UID;

import org.hibernate.Query;
import org.apache.log4j.Logger;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class PersonDAO_HibernateImpl extends HibernateDaoSupport implements
		PersonDAO {
	static Logger logger = Logger.getLogger(PersonDAO_HibernateImpl.class);

	public void delete(Person obj) {
		try {
			this.getHibernateTemplate().delete(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Person findByPK(String pk) {
		return (Person) this.getHibernateTemplate().get(Person.class, pk);
	}

	public String create(Person obj) {
		String pk = UID.getUID();
		obj.setPersonPk(pk);
		try {
			this.getHibernateTemplate().save(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pk;
	}

	public void update(Person obj) {
		try {
			this.getHibernateTemplate().update(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private Query getQuery(String str, PersonQuery quester) {
		StringBuffer hql = new StringBuffer(str);
		if (quester != null) {
			if (quester.getFullName() != null
					&& !"".equals(quester.getFullName())) {
				hql.append(" and fullName like :fullName");
			}
			
			if (quester.getDepartmentName() != null
					&& !"".equals(quester.getDepartmentName())) {
				hql.append(" and department.nameMap like :nameMap");
			}

		}

		Query query = this.getSession().createQuery(hql.toString());
		if (quester != null) {
			if (quester.getFullName() != null
					&& !"".equals(quester.getFullName())) {
				query.setString("fullName", "%"
						+ String.valueOf(quester.getFullName()) + "%");
			}
			if (quester.getDepartmentName() != null
					&& !"".equals(quester.getDepartmentName())) {
				query.setString("nameMap", "%"
						+ String.valueOf(quester.getDepartmentName()) + "%");
			}
		}
		return query;
	}

	public int getAllCount_findByQuery(PersonQuery quester) {
		Long allcount = new Long(0);
		String hql = "select count(person) from Person person where 1=1";
		try {
			Query query = getQuery(hql, quester);
			Iterator result = query.iterate();
			if (result.hasNext()) {
				allcount = (Long) result.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return allcount.intValue();
	}

	public List findByQuery(PersonQuery quester, int start, int count) {
		String hql = "select person from Person person where 1=1";
		try {
			Query query = getQuery(hql, quester);
			query.setFirstResult(start);
			query.setMaxResults(count);

			return query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public Person findByPersonNo(String no) {
		List list = null;
		String hql_find_by_no = "from Person where personNo=?";
		try {
			list = this.getHibernateTemplate().find(hql_find_by_no, no);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (list != null && list.isEmpty()) {
			return null;
		} else {
			Iterator it = list.iterator();
			Person person = (Person) it.next();
			return person;
		}
	}

	public boolean existPersonNo(String no, String pk) {
		boolean flag = false;
		if (no == null || "".equals(no.trim())) {
			flag = false;
		} else {
			Long count = new Long(0);
			StringBuffer hql = new StringBuffer(
					"select count(person) from Person person where personNo='"
							+ no + "'");

			if (pk != null && !"".equals(pk.trim())) {
				hql.append(" and personPk != '" + pk + "'");
			}

			try {
				Iterator result = this.getHibernateTemplate().find(
						hql.toString()).iterator();
				if (result.hasNext()) {
					count = (Long) result.next();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

			if (count.equals(new Long(0))) {
				flag = false;
			} else {
				flag = true;
			}
		}
		return flag;
	}
}
